import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.TriggerBuilder;

import java.util.concurrent.*;

public class TestFuture {

    public static void main(String[] args){
        ExecutorService service = Executors.newCachedThreadPool();

        Task task = new Task();

        Future<Integer> result = service.submit(task);

        service.shutdown();


        CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                //设置Cron表达式
                //从0秒开始 每隔20秒执行一次
                .withSchedule(CronScheduleBuilder.cronSchedule("0/20 * * * * ?"))
                .build();


//        try{
////            Thread.sleep(1000);
//        }catch (InterruptedException e){
//            e.printStackTrace();
//        }

        try {
            System.out.println("task运行结果"+result.get(1, TimeUnit.MILLISECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            System.out.println("超时了");
            e.printStackTrace();
        }

        System.out.println("所有任务执行完毕");
    }
}

class Task implements Callable<Integer>{

    public Integer call() throws Exception {
        System.out.println("子线程在进行计算");
        Thread.sleep(6000);
        int sum = 0;
        for(int i=0;i<100;i++)
            sum += i;
        System.out.println("子线程计算结束:"+sum);
        return sum;
    }
}